package com.ssy.lingxi.product.entity.commodity;

import com.fasterxml.jackson.annotation.JsonManagedReference;
import com.ssy.lingxi.product.api.model.request.commodity.LogisticsRequest;
import com.ssy.lingxi.product.entity.Brand;
import com.ssy.lingxi.product.entity.commodity.member.CommodityUnitPriceStrategy;
import com.ssy.lingxi.product.entity.customer.CustomerCategory;
import com.ssy.lingxi.product.handler.converter.JpaJsonToLogisticsConverter;
import com.ssy.lingxi.product.handler.converter.JpaStringToArrayConverter;
import lombok.Getter;
import lombok.Setter;

import javax.persistence.*;
import java.io.Serializable;
import java.util.List;

/**
 * 商品实体类
 * @author jw.chen
 * @version 2.0.0
 * @date 2020/6/28
 */
@Getter
@Setter
@Entity
@Table(schema = "public", name = "product_commodity", indexes = {@Index(name = "product_commodity_member_id_member_role_id_idx", columnList = "memberId,memberRoleId"),
        @Index(name = "product_commodity_status_idx", columnList = "status"),
        @Index(name = "product_commodity_code_idx", columnList = "code"),
        @Index(name = "product_commodity_name_idx", columnList = "name"),
        @Index(name = "product_commodity_price_type_idx", columnList = "priceType"),
        @Index(name = "product_commodity_publish_time_idx", columnList = "publishTime"),
        @Index(name = "product_commodity_store_id_idx", columnList = "storeId")})
public class Commodity implements Serializable {
    private static final long serialVersionUID = 7067421985999729060L;
    /**
     * 主键id
     */
    @Id
    @GeneratedValue(strategy= GenerationType.SEQUENCE, generator="commodity_seq")
    @SequenceGenerator(sequenceName="product_commodity_seq", name="commodity_seq",allocationSize = 1)
    private Long id;

    /**
     * 审核状态: 1-待提交审核, 2-待审核, 3-审核不通过, 4-审核通过, 5-上架, 6-下架
     */
    @Column(columnDefinition = "int", nullable = false)
    private Integer status = 1;

    /**
     * 申请审核时间
     */
    @Column(columnDefinition = "int8")
    private Long applyTime;

    /**
     * 会员品类
     */
    @OneToOne(cascade = {CascadeType.PERSIST})
    @JoinColumn(name="customerCategoryId",referencedColumnName = "id")
    private CustomerCategory customerCategory;

    /**
     * 品牌id
     */
    @OneToOne(cascade = {CascadeType.PERSIST})
    @JoinColumn(name="brandId",referencedColumnName = "id")
    private Brand brand;

    /**
     * 商品编码
     */
    @Column(columnDefinition = "varchar(10)")
    private String code;

    /**
     * 商品名称
     */
    @Column(columnDefinition = "varchar(90)", nullable = false)
    private String name;

    /**
     * 商品标语
     */
    @Column(columnDefinition = "varchar(90)")
    private String slogan;

    /**
     * 商品卖点
     */
    @Convert(converter = JpaStringToArrayConverter.class)
    @Column(columnDefinition = "varchar(150)")
    private String[] sellingPoint;

    /**
     * 是否不限制区域
     */
    @Column(columnDefinition = "boolean")
    private Boolean isAllArea = true;

    /**
     * 归属地市
     */
    @JsonManagedReference
    @OneToMany(mappedBy = "commodity", cascade={CascadeType.PERSIST, CascadeType.MERGE, CascadeType.REMOVE})
    private List<CommodityArea> commodityAreaList;

    /**
     * 是否允许修改商品规格属性
     */
    @Column(columnDefinition = "boolean")
    private Boolean isUpdateAttribute = true;

    /**
     * 商品属性
     */
    @JsonManagedReference
    @OneToMany(mappedBy = "commodity", cascade={CascadeType.PERSIST, CascadeType.MERGE, CascadeType.REMOVE})
    private List<CommodityAttribute> commodityAttributeList;

    /**
     * 计量单位id
     */
    @Column(columnDefinition = "int8", nullable = false)
    private Long unitId;

    /**
     * 计量单位
     */
    @Column(columnDefinition = "varchar(20)", nullable = false)
    private String unitName;

    /**
     * 副单位计量单位id
     */
    @Column(columnDefinition = "int8")
    private Long subUnitId;

    /**
     * 副单位计量单位
     */
    @Column(columnDefinition = "varchar(20)")
    private String subUnitName;

    /**
     * 最小起订
     */
    @Column(columnDefinition = "decimal")
    private Double minOrder;

    /**
     * 产品定价：1-现货价格, 2-价格需要询价, 3-积分兑换商品, 4-赠品
     */
    @Column(columnDefinition = "int", nullable = false)
    private Integer priceType;

    /**
     * 是否允许使用会员折扣价购买
     */
    @Column(columnDefinition = "boolean", nullable = false)
    private Boolean isMemberPrice = true;

    /**
     * 最小(价格/积分)
     */
    @Column(columnDefinition = "decimal")
    private Double min;

    /**
     * 最大(价格/积分)
     */
    @Column(columnDefinition = "decimal")
    private Double max;

    /**
     * 税率
     */
    @Column(columnDefinition = "decimal")
    private Double taxRate = 0d;

    /**
     * 是否所有属性共用图片
     */
    @Column(columnDefinition = "boolean", nullable = false)
    private Boolean isAllAttributePic = true;

    /**
     * 商品主图
     */
    @Column(columnDefinition = "varchar(250)", nullable = false)
    private String mainPic;

    /**
     * 商品单价和图片
     */
    @JsonManagedReference
    @OneToMany(mappedBy = "commodity", cascade={CascadeType.PERSIST, CascadeType.MERGE, CascadeType.REMOVE})
    private List<CommodityUnitPriceAndPic> unitPriceAndPicList;

    /**
     * 是否存在价格策略
     */
    @Column(columnDefinition = "boolean")
    private Boolean isUnitPriceStrategy = false;

    /**
     * 商品价格策略
     */
    @JsonManagedReference
    @OneToMany(mappedBy = "commodity", cascade={CascadeType.PERSIST, CascadeType.MERGE, CascadeType.REMOVE})
    private List<CommodityUnitPriceStrategy> unitPriceStrategyList;

    /**
     * 是否跨境商品
     */
    @Column(columnDefinition = "boolean")
    private Boolean isCrossBorder = false;

    /**
     * 送货周期
     */
    @Column(columnDefinition = "int")
    private Integer sendCycle;

    /**
     * 商品描述
     */
    @OneToOne(cascade = {CascadeType.PERSIST, CascadeType.MERGE, CascadeType.REMOVE})
    @JoinColumn(name="commodityRemarkId",referencedColumnName = "id")
    private CommodityRemark commodityRemark;

    /**
     * 物流信息
     */
    @Convert(converter = JpaJsonToLogisticsConverter.class)
    @Column(columnDefinition="jsonb")
    private LogisticsRequest logistics;

    /**
     * 是否开发票
     */
    @Column(columnDefinition = "boolean")
    private Boolean isInvoice;

    /**
     * 唛头
     */
    @Column(columnDefinition = "varchar(50)")
    private String marks;

    /**
     * 包装清单
     */
    @Column(columnDefinition = "varchar(120)")
    private String packing;

    /**
     * 售后服务
     */
    @Column(columnDefinition = "varchar(300)")
    private String afterService;

    /**
     * 会员id
     */
    @Column(columnDefinition = "int8", nullable = false)
    private Long memberId;

    /**
     * 会员名称
     */
    @Column(columnDefinition = "varchar(50)", nullable = false)
    private String memberName;

    /**
     * 会员角色id
     */
    @Column(columnDefinition = "int8", nullable = false)
    private Long memberRoleId;

    /**
     * 会员角色名称
     */
    @Column(columnDefinition = "varchar(50)", nullable = false)
    private String memberRoleName;

    /**
     * 用户id
     */
    @Column(columnDefinition = "int8", nullable = false)
    private Long userId;

    /**
     * 用户名称
     */
    @Column(columnDefinition = "varchar(50)", nullable = false)
    private String userName;

    /**
     * 创建时间
     */
    @Column(columnDefinition = "int8", nullable = false)
    private Long createTime = System.currentTimeMillis();

    /**
     * 审核时间
     */
    @Column(columnDefinition = "int8")
    private Long checkTime;

    /**
     * 上架时间
     */
    @Column(columnDefinition = "int8")
    private Long publishTime;

    /**
     * 商品发布的商城
     */
    @JsonManagedReference
    @OneToMany(mappedBy = "commodity", cascade={CascadeType.PERSIST, CascadeType.MERGE, CascadeType.REMOVE})
    private List<CommodityShop> commodityShopList;

    /**
     * 店铺id
     */
    @Column(columnDefinition = "int8")
    private Long storeId;

    /**
     * 店铺名称
     */
    @Column(columnDefinition = "varchar(100)")
    private String storeName;

    /**
     * 店铺logo
     */
    @Column(columnDefinition = "varchar(250)")
    private String storeLogo;

    /**
     * 导入批次
     */
    @Column(columnDefinition = "varchar(50)")
    private String importBatch;

    /**
     * SEO优化: title
     */
    @Column(columnDefinition = "varchar(50)")
    private String title;

    /**
     * SEO优化: description
     */
    @Column(columnDefinition = "varchar(200)")
    private String description;

    /**
     * SEO优化: keywords
     */
    @Column(columnDefinition = "varchar(100)")
    private String keywords;

    /**
     * 商品类型-CommodityTypeEnum: 1-自营商品; 2-上游商品; 3-代销商品;
     */
    @Column(columnDefinition = "int")
    private Integer type = 1;

    /**
     * 商品来源-CommodityS2BSourceEnum: 1-上游供应商品; 2-下游销售商品;
     * S2B选择上游供应商品/下游销售商品功能使用 其他渠道则为空
     */
    @Column(columnDefinition = "int")
    private Integer sourceType;

    /**
     * 上游商品id
     */
    @Column(columnDefinition = "int8")
    private Long upperCommodityId;

    /**
     * 上游供应会员id
     */
    @Column(columnDefinition = "int8")
    private Long upperMemberId;

    /**
     * 上游供应会员名称
     */
    @Column(columnDefinition = "varchar(50)")
    private String upperMemberName;

    /**
     * 上游供应会员角色id
     */
    @Column(columnDefinition = "int8")
    private Long upperMemberRoleId;

    /**
     * 上游供应会员角色名称
     */
    @Column(columnDefinition = "varchar(50)")
    private String upperMemberRoleName;

    /**
     * 审核类型: 1-会员审核, 2-平台审核
     */
    @Column(columnDefinition = "int", nullable = false)
    private Integer checkType = 1;
}
